
import java.io.*;

import org.antlr.runtime.*;
import org.antlr.runtime.debug.DebugEventSocketProxy;

import ast.Program;

import util.ConsoleErrorReporter;
import util.IErrorReporter;

/**
 * 
 * 
 * 
 * @author Fagner Araujo
 * 
 * SVN muito bom.
 *
 */

public class Main {

    public static void main(String args[]) throws Exception {
    	try {
    		
    		
			ANTLRFileStream input = new ANTLRFileStream("entrada.txt");
			
			gramaticaMM2Lexer lexer = new gramaticaMM2Lexer(input);
			CommonTokenStream tokenStream = new CommonTokenStream(lexer);
			
			System.out.println("Parsing...");
			
			gramaticaMM2Parser parser = new gramaticaMM2Parser(tokenStream);
			parser.setTreeAdaptor(new MyTreeAdaptor());
			Program program = (Program) parser.programa().getTree();

			System.out.println("Syntatic Analysis...");

           // Analize contextual
            IErrorReporter errorReporter = new ConsoleErrorReporter();
            MyChecker checker = new MyChecker(errorReporter);
            checker.visit(program, null);

            if (errorReporter.getErrors() == 0)
            {
//            	System.out.println("Code generation...");
//
//              AulaCodeGenerator codeGenerator = new AulaCodeGenerator();
//                codeGenerator.visit(program, null);

                System.out.println("Compilation succeeded!");
            }
            else
            {
            	System.out.printf("Compilation failed with %d errors", errorReporter.getErrors());
            }
		} catch (IOException e) {			
			e.printStackTrace();
	   } 	catch (RecognitionException e) {
			e.printStackTrace();
		}
	}
}